#!/usr/bin/python
# -*- coding: utf-8 -*-

import gluon.contrib.simplejson as sj
import random
# # sample index page with internationalization (T)
def index():
    #response.flash = T('Welcome to web2py')
    return dict(message=T('Hello World'))

def addMessage():
    """
    
    """
    content=request.vars.content
    postfrom=request.vars.postfrom
    db.message.insert(content=content,postfrom=postfrom)
    result={}
    result['success']=True
    data={}
    data['content']=content
    data['postfrom']=postfrom
    data['x']=random.randint(10,900)
    data['y']=random.randint(160,450)
    result['data']=data
    return sj.dumps(result)

def getAllMessage():
    messages=db().select(db.message.ALL,orderby=db.message.posttime)
    def makeJson(data):
        d={}
        d['content']=data['content']
        d['postfrom']=data['postfrom']
        d['x']=random.randint(10,900)
        d['y']=random.randint(160,700)
        return d
    result={}
    result['data']=[makeJson(x) for x in messages]
    result['success']=True
    return sj.dumps(result)

    

# # uncomment the following if you have defined "auth" and "crud" in models
# def user(): return dict(form=auth())
# def data(): return dict(form=crud())
# def download(): return response.download(request,db)
# # tip: use @auth.requires_login, requires_membership, requires_permission
